// subPages/shopPage/shoppingCartOrder/shoppingCartOrder.js
var app = getApp();
var utils = require('../../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      orderPrice:0
  },
    submitOrder(){
        // var product = [{
        //     shop_id:this.data.goodsInfo.shop_id,
        //     content_id:this.data.goodsInfo.class_id,
        //     num:this.data.buyNum
        // }];
        // product = JSON.stringify(product);
        var that = this;
        var buyList = this.data.buyList;
        var product = [];
        for(let i=0;i<buyList.length;i++){
            var obj = {
                shop_id:that.data.buyList[i].shop.id,
                content_id:that.data.buyList[i].content.id,
                num:that.data.buyList[i].num
            }
            product.push(obj);
        }
        product = JSON.stringify(product);
        var userid = app.globalData.userId;
        var orderPrice = this.data.orderPrice;
        var orderJf = this.data.totalPrice;
        var address = this.data.address;
        var sheng = address.sheng;
        var shi = address.shi;
        var qu = address.qu;
        var name = address.name;
        var phone = address.phone;
        address = address.address;
        var data = {
            product:product,
            userid:userid,
            order_price:orderPrice,
            order_jf:orderJf,
            address:address,
            sheng:sheng,
            shi:shi,
            qu:qu,
            name:name,
            phone:phone
        }
        // console.log(product,userid,orderJf,orderPrice,address);
        wx.showModal({
            title:'交易提示',
            content:'是否确认兑换该商品？',
            success(res){
                if(res.confirm){
                    utils.postAjax(app.globalData.apiUrl+'/user_pay',data,function(res){
                        // res.data 2 积分不足   1成功  0失败
                        console.log(res);
                        if(res.data==1){
                            wx.showToast({
                                title:'兑换成功！',
                                icon:'none',
                                duration:2000
                            })
                        }else if(res.data==2){
                            wx.showToast({
                                title:'积分不足，兑换失败',
                                icon:'none',
                                duration:2000
                            })
                        }else if(res.data==0){
                            wx.showToast({
                                title:'兑换失败，请重新兑换！',
                                icon:'none',
                                duration:1000
                            })
                        }
                    })
                }else if(res.cancel){}
            }
        })
    },
    selectAddress(){
        var addressId = 1;
        wx.navigateTo({
            url:"../addressManager/index"
        })
    },
    totalPrice(){
      var that = this;
      var buyList = this.data.buyList;
      var totalPrice = 0;
      for(let i=0;i<buyList.length;i++){
          totalPrice += buyList[i].num*buyList[i].content.price;
      }
      that.setData({
          totalPrice:totalPrice
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var that = this;
     wx.getStorage({
         key:'shoppingCartBuy',
         success(res){
             console.log(res);
             that.setData({
                 buyList:res.data
             })
         }
     })
      utils.sendAjax(app.globalData.apiUrl+'/user_get_address','GET',function(res){
          var address = res.data
          for(var i=0;i<address.length;i++){
              if(address[i].status == 1){
                  that.setData({
                      addressId:address[i].id
                  })
                  wx.setStorageSync('addressId',address[i].id)
              }
          }
      },{userid:app.globalData.userId})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that = this;
      var addressId = '';
      setTimeout(function(){
          var addressId = wx.getStorageSync('addressId');
          console.log(addressId);
          utils.sendAjax(app.globalData.apiUrl+'/get_address_content','GET',function(res){
              console.log(res);
              that.setData({
                  address:res.data
              })
          },{id:addressId})
      },200)
      setTimeout(function(){
          that.totalPrice();
      },200)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
